<?php

namespace My\EhrBundle\Lib;

class Pager {

    private $_currentPage   = 1,
            $_recordsCount  = 0,
            $_totalPages    = 0,
            $_perPage       = 10,
            $_offsetQuery   = 0,
            $_pagination    = null;

    public function pagination()
    {
        return $this->_pagination;
    }
    public function __construct( $recordsCount, $currentPage = 1, $perPage = 10 )
    {
       $this->_recordsCount = (int)$recordsCount;
       $this->_perPage      = (int)$perPage;
       $this->_currentPage  = (int)$currentPage;
       $this->_pagination   = new Pagination($this);

       $this->_initialize();
    }
    /**
     * @todo Return Current Offset Of the Current Page
     * @return <integer>
     */
    public function getOffsetQuery()
    {
        return $this->_offsetQuery;
    }
    private function _initialize()
    {
		if( $this->_perPage < 1 )
		{
			$this->_perPage = 10;
		}
        if( $this->_currentPage < 1 )
        {
            $this->_currentPage = 1;
        }

        $this->_totalPages = \ceil ($this->_recordsCount / $this->_perPage);

//        if( $this->_recordsCount > $this->_perPage )
//        {
//            if( $this->_recordsCount % $this->_perPage > 0 )
//            {
//                ++$this->_totalPages;
//            }
//        }
        if( $this->_currentPage > $this->_totalPages )
        {
            $this->_currentPage = $this->_totalPages;
        }
        $this->_offsetQuery = $this->_perPage * $this->_currentPage - $this->_perPage;

        if( $this->_offsetQuery < 0 )
        {
            $this->_offsetQuery = 0;
        }

    }
    /**
     * @return <boolean>
     */
    public function hasNext()
    {
        return $this->_currentPage < $this->getTotalPages();
    }
    /**
     * @return <boolean>
     */
    public function hasPrevious()
    {
        return $this->_currentPage > $this->getFirstPage();
    }
    /**
     * @param $perPage <integer>
     * @return My\EhrBundle\Lib\Pager
     */
    public function setPerPage($perPage)
    {
        $this->_perPage = (int)$perPage;
        $this->_initialize();
        return $this;
    }
    /**
     * @return <integer>
     */
    public function getPerPage()
    {
        return $this->_perPage;
    }
    /**
     * @return <integer>
     */
    public function getPreviousPage()
    {
        $previousPage = $this->_currentPage - 1;
        return $previousPage < $this->getFirstPage() ? $this->getFirstPage() : $previousPage;
    }
    /**
     * @return <integer>
     */
    public function getStartNumber()
    {
        $startNumber = 0;

        if( $this->_currentPage > 1 )
        {
            $startNumber = $this->_perPage * $this->getCurrentPage() - 1;
        }
        return $startNumber;
    }
    /**
     * @return <integer>
     */
    public function getNextPage()
    {
        $nextPage = $this->_currentPage + 1;    
        return $nextPage > $this->_totalPages ? $this->_totalPages : $nextPage;
    }
    /**
     * @return <integer>
     */
    public function getFirstPage()
    {
        return 1;
    }
    /**
     * @return <integer>
     */
    public function getLastPage()
    {
        return $this->getTotalPages();
    }
    /**
     * @return <integer>
     */
    public function getCurrentPage()
    {
        return $this->_currentPage;
    }
    /**
     * @return <integer>
     */
    public function getTotalPages()
    {
        return $this->_totalPages;
    }
}
?>
